package de.karlNet.gifSocket.client;

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class Screenshotter {

	/**
	 * @param args
	 * @throws AWTException
	 * @throws IOException
	 * @throws ImageFormatException
	 * @throws InterruptedException
	 */
	public static void main(String[] args) throws AWTException,
			ImageFormatException, IOException, InterruptedException {
		getStream();
		System.exit(0);
		Integer sessionNumber = getSessionNumber();
		System.out.println(sessionNumber);
		Rectangle captureSize = new Rectangle(Toolkit.getDefaultToolkit()
				.getScreenSize());
		Robot robot = new Robot();
		ByteArrayOutputStream myBAOS = new ByteArrayOutputStream();
		JPEGImageEncoder myEncoder = JPEGCodec.createJPEGEncoder(myBAOS);
		while (true) {

			final BufferedImage bufferedImage = (BufferedImage) robot
					.createScreenCapture(captureSize);

			myEncoder.encode(bufferedImage);
			byte[] myByteArray = myBAOS.toByteArray();
			myBAOS.reset();
			InputStream myIS = new ByteArrayInputStream(myByteArray);

			HttpClient httpclient = new DefaultHttpClient();
			httpclient.getParams().setParameter(
					CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
			HttpPost httppost = new HttpPost(
					"http://localhost:8080/gif/upload?session_id="
							+ sessionNumber);

			MultipartEntity mpEntity = new MultipartEntity(
					HttpMultipartMode.BROWSER_COMPATIBLE);
			InputStreamBody uploadFilePart = new InputStreamBody(myIS,
					"application/octet-stream", "test.file");
			mpEntity.addPart("file", uploadFilePart);
			httppost.setEntity(mpEntity);

			HttpResponse response = httpclient.execute(httppost);
		}

	}

	private static void getStream() throws IllegalStateException, IOException {
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(
				"http://localhost:8080/gif/click");

		HttpResponse response = httpclient.execute(httpGet);
		InputStream stream = response.getEntity().getContent();
		int readByte = -1;
		do {
			readByte = stream.read();
			System.out.println(readByte);
		} while(readByte!=-1);
	}

	private static int getSessionNumber() throws ClientProtocolException,
			IOException {
		Rectangle captureSize = new Rectangle(Toolkit.getDefaultToolkit()
				.getScreenSize());
		HttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter(
				CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
		HttpGet httpGet = new HttpGet(
				"http://localhost:8080/gif/create_session?x="
						+ captureSize.width + "&y=" + captureSize.height);

		HttpResponse response = httpclient.execute(httpGet);

		String responseString = EntityUtils.toString(response.getEntity(),
				"UTF-8");
		return Integer.parseInt(responseString);
	}
}
